package com.lineage.data.item_etcitem.extra;

import com.lineage.config.ConfigAlt;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_DragonDoor;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.world.World;
import java.util.Iterator;

public class DragonKey extends ItemExecutor {
  public static ItemExecutor get() {
    return new DragonKey();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int castleId = L1CastleLocation.getCastleIdByArea((L1Character)pc);
    if (castleId > 0 && ServerWarExecutor.get().isNowWar(castleId)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1892));
      return;
    } 
    boolean isChecked = false;
    Iterator<?> localIterator1 = ConfigAlt.DRAGON_KEY_MAP_LIST.iterator();
    while (localIterator1.hasNext()) {
      int mapid = ((Integer)localIterator1.next()).intValue();
      if (mapid == pc.getMapId()) {
        isChecked = true;
        break;
      } 
    } 
    if (!isChecked) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1892));
      return;
    } 
    int a = 6;
    int b = 6;
    int c = 6;
    int d = 0;
    Iterator<L1Object> iterator = World.get().getObject().iterator();
    while (iterator.hasNext()) {
      L1Object object = iterator.next();
      if (object instanceof L1NpcInstance) {
        L1NpcInstance npc = (L1NpcInstance)object;
        if (npc.getNpcTemplate().get_npcId() == 70932) {
          a--;
          continue;
        } 
        if (npc.getNpcTemplate().get_npcId() == 70937) {
          b--;
          continue;
        } 
        if (npc.getNpcTemplate().get_npcId() != 70934)
          continue; 
        c--;
      } 
    } 
    pc.sendPackets((ServerBasePacket)new S_DragonDoor(item.getId(), a, b, c, 0));
  }
}
